package struts.form;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author ily FormFactory
 */
public class FormFactory {
	private static final Logger logger = Logger.getLogger(ActionFactory.class);

	@SuppressWarnings("rawtypes")
	public static ActionForm createForm(String formPath, HttpServletRequest request) {
		logger.info("************** 装载" + formPath + "!*************");
		ActionForm actionForm = null;
		try {
			Class clazz = Class.forName(formPath);
			actionForm = (ActionForm) clazz.newInstance();
			Field[] propertys = clazz.getDeclaredFields();
			for (Field field : propertys) {
				field.setAccessible(true);
				field.set(actionForm, request.getParameter(field.getName()));
				field.setAccessible(false);
			}
		} catch (Exception e) {
			logger.info("**************FORM 装载失败!*************");
			logger.error(e);
		}
		return actionForm;
	}
}
